package com.rm2pt.generator.springboot.LabManage.service;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rm2pt.generator.springboot.LabManage.entity.*;
import com.rm2pt.generator.springboot.LabManage.utils.DaoManage;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.ServiceManage;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import com.rm2pt.generator.springboot.LabManage.redis.CurrentUtils;

@Transactional
@Service
public class CreatePlanService{
	@Autowired CurrentUtils currentUtils;
	@Autowired private DaoManage DM;
		
	@Autowired private ServiceManage serviceManage;

	public Boolean createPlan(String groupId,String id,String studentId,String content,Boolean finish) throws PreconditionException{
		Plan plan = (Plan)GetData(DM.getPlanDao().findById(id));
		StudentInGroup sig = DM.getStudentInGroupDao().findByGroupIdAndStudentId(groupId,studentId);
		
		if(StandardOPs.oclIsUndefined(plan) == true && StandardOPs.oclIsUndefined(sig) == false)
		{
			Plan p = new Plan();
			p.setId(id);
			p.setBelongUser(studentId);
			p.setBelongGroup(groupId);
			p.setContent(content);
			p.setFinish(false);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean editPlan(String id,String content) throws PreconditionException{
		Plan plan = (Plan)GetData(DM.getPlanDao().findById(id));
		
		if(StandardOPs.oclIsUndefined(plan) == false)
		{
			plan.setContent(content);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean finishPlan(String id,Boolean finish) throws PreconditionException{
		Plan plan = (Plan)GetData(DM.getPlanDao().findById(id));
		
		if(StandardOPs.oclIsUndefined(plan) == false)
		{
			plan.setFinish(finish);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
				
	public static Object GetData(Optional<?> op) {
		if (op.isPresent())
			return op.get();
		else 
			return null;
	}
}
